package org.openkoala.koala.deploy.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.widgets.Shell;
import org.openkoala.koala.deploy.action.SoapWebServiceDeployAction;
import org.openkoala.koala.deploy.pojo.InterfaceTree;
import org.openkoala.koala.deploy.pojo.Itree;
import org.openkoala.koala.deploy.pojo.LocalRemoteTree;
import org.openkoala.koala.deploy.pojo.MethodTree;
import org.openkoala.koala.deploy.webservice.pojo.InterfaceObj;
import org.openkoala.koala.deploy.webservice.pojo.WebServiceDeploy;
import org.openkoala.koala.deploy.webservice.pojo.WebServiceMethod;
import org.openkoala.koala.java.JavaManagerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SoapWebServiceSelectionUI extends SelectionUI<WebServiceDeploy> {

	
	private static final Logger logger = LoggerFactory
			.getLogger(SoapWebServiceSelectionUI.class);
	
	private SoapWebServiceDeployAction action;
	
	private WebServiceDeploy webServiceDeploy;

	public SoapWebServiceSelectionUI(Shell shell,
			SoapWebServiceDeployAction webServiceDeployAction) {
		super(shell,"发布SOAP WebService服务");
		this.action = webServiceDeployAction;
	}

	@Override
	public void initData(WebServiceDeploy webServiceDeploy) {
		this.webServiceDeploy = webServiceDeploy;
		java.util.List<Itree> trees = new ArrayList<Itree>();
		Itree webService = LocalRemoteTree.createWebServiceTreeNode();
		trees.add(webService);
		copyDeploy(webService,webServiceDeploy);
		treeViewer.setInput(trees);
		treeViewer.expandToLevel(2);
		treeViewer.refresh();
	}

	private void copyDeploy(Itree tree, WebServiceDeploy webServiceDeploy) {
		logger.info("解析成功，初始化数据"+webServiceDeploy);
		List<InterfaceObj> interfaces = webServiceDeploy.getInterfaces();
		for (InterfaceObj inter : interfaces) {
			InterfaceTree interfaceTree = new InterfaceTree(inter.getName());
			for (WebServiceMethod method : inter.getMethods()) {
				MethodTree methodTree = new MethodTree(JavaManagerUtil.methodDescription(method.getMethod()));
//				methodTree.setType(method.getType());
				interfaceTree.getChildren().add(methodTree);
			}
			tree.getChildren().add(interfaceTree);
		}
	}
	
	@Override
	public void process() {
		Map<String,List<String>> selectedMap = new HashMap<String,List<String>>();
		Map<String,List<MethodTree>> selectedTrees = new HashMap<String,List<MethodTree>>();
		List<Itree> trees = (List)this.treeViewer.getInput();
		for(Itree tree:trees){
			List<InterfaceTree> interfaces = (List<InterfaceTree>)tree.getChildren();
			for(InterfaceTree interTree:interfaces){
				if(interTree.isCheck()){
					selectedMap.put(interTree.getName(), interTree.getSelectedChildren());
					selectedTrees.put(interTree.getName(), interTree.getSelectedTree());
				}
			}
		}
		
		List<InterfaceObj> interfaces = new ArrayList<InterfaceObj>(webServiceDeploy.getInterfaces());
		
		for(InterfaceObj inter : interfaces){
			String name = inter.getName();
			List<String> methods = new ArrayList<String>();
			if(selectedMap.containsKey(name)){
				methods = selectedMap.get(name);
				inter.setSelectedMethods(methods);
			}else{
				webServiceDeploy.getInterfaces().remove(inter);
			}
		}
		action.setWebServiceDeploy(webServiceDeploy);
		super.close();
		action.process();
	}
}
